package com.ahora.org.controller.biometrico.servlet;



import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ahora.org.controller.util.Estatica;
import com.ahora.org.ejb.dao.util.ConexionBD;
import com.ahora.org.ejb.jpa.beans.UsuarioBiometrico;

/**
 * Servlet implementation class BiometricoServlet
 */
@WebServlet("/ServletIdentificador")
public class BiometricoIdentificacionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private UsuarioBiometrico peticionInformacion(HttpServletRequest request) {
		UsuarioBiometrico myrespObj = null;
		UsuarioBiometrico myObject = null;
		try {
			ObjectInputStream inputFromApplet = new ObjectInputStream(
					request.getInputStream());
			myObject = (UsuarioBiometrico) inputFromApplet.readObject();
			Connection con = null;
			Statement stmt = null;
			ResultSet rs = null;
			try {

				con = ConexionBD.getConnection();
				stmt = con.createStatement();
				rs = stmt.executeQuery("SELECT ARRAY_BIOMETRICO, TAMANIO, NOMBRES, APELL_PATER, APELL_MATER, ID_USUARIO " +
										"FROM TB_USUARIO " +
										"WHERE ID_USUARIO="+Estatica.getUsuarioVerificar().getIdUsuario());
				
				if (rs.next()) {
					myrespObj = new UsuarioBiometrico();
					myrespObj.setArreglo(rs.getBytes(1));
					myrespObj.setTamanio(rs.getInt(2));
					myrespObj.setNombre(rs.getString(3).trim());
					myrespObj.setApellido(rs.getString(4).trim()+" "+rs.getString(5).trim());
					myrespObj.setId(rs.getInt(6));
					System.out.println("Entre");
				} 
				rs.close();
				stmt.close();
				con.close();

			} catch (Exception e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return myrespObj;
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("Llamaron a mi servlet jejje");
		try {
			UsuarioBiometrico myrespObj = peticionInformacion(request);
			response.reset();
			response.setHeader("Content-Type",
					"application/x-java-serialized-object");
			ObjectOutputStream outputToApplet;
			outputToApplet = new ObjectOutputStream(response.getOutputStream());
			outputToApplet.writeObject(myrespObj);
			outputToApplet.flush();
			outputToApplet.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
